{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "61ef6f1f",
   "metadata": {},
   "source": [
    "Python 标准库提供了两个特别有用的模块，可以辅助调查代码的性能问题。\n",
    "\n",
    "一个是 timeit 模块---该模块可用于对一小段 Python 代码进行计时，并可用于诸如对两个或多个特定函数或方法的性能进行比较等场合。\n",
    "\n",
    "另一个是 cProfile 模块，可用于 profile程序的性能--该模块对调用计数与次数进行了详细分解，以便发现性能瓶颈所在。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "220661c6",
   "metadata": {},
   "source": [
    "#  timeit 模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98ca3316",
   "metadata": {},
   "outputs": [],
   "source": [
    "import timeit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10a71c20",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(timeit.timeit('max(2, 3, 1, -5)',number=1000000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08a141c7",
   "metadata": {},
   "source": [
    "### timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000)\n",
    "     \n",
    "stmt: 这个参数就是statement,可以把要进行计算时间的代码放在里面。他可以直接接受字符串的表达式，也可以接受单个变量，也可以接受函数。\n",
    "\n",
    "setup:  这个参数可以将stmt的环境传进去。比如各种import和参数什么的。\n",
    "\n",
    "timer: 这个参数一般使用不到，具体使用可以参看文档。\n",
    "    \n",
    "number:每一次测量中语句被执行的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ddfb623f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.819960108\n",
      "7.227565770000002\n"
     ]
    }
   ],
   "source": [
    "import timeit\n",
    "foooo = \"\"\"\n",
    "sum = []\n",
    "for i in range(1000):\n",
    "    sum.append(i)\n",
    "\"\"\"\n",
    " \n",
    "print(timeit.timeit(stmt=\"[i for i in range(1000)]\", number=100000))\n",
    "print(timeit.timeit(stmt=foooo, number=100000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4f0e2d0",
   "metadata": {},
   "source": [
    "# cprofile 模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "148ae0df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         4 function calls in 0.076 seconds\n",
      "\n",
      "   Ordered by: standard name\n",
      "\n",
      "   ncalls  tottime  percall  cumtime  percall filename:lineno(function)\n",
      "        1    0.076    0.076    0.076    0.076 <ipython-input-4-02973dae4eb4>:2(func1)\n",
      "        1    0.000    0.000    0.076    0.076 <string>:1(<module>)\n",
      "        1    0.000    0.000    0.076    0.076 {built-in method builtins.exec}\n",
      "        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import cProfile\n",
    "def func1():\n",
    "    sum = 0\n",
    "    for i in range(1000000):\n",
    "        sum += i\n",
    "cProfile.run('func1()')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1d2a670",
   "metadata": {},
   "source": [
    "第一行：4个函数调用被监控，其中4个是原生调用（不涉及递归）\n",
    "\n",
    "ncalls:表示函数调用的次数;如果这一列有两个值，就表示有递归调用，第二个值是原生调用次数，第一个值是总调用次数\n",
    "\n",
    "tottime:表示指定函数的总的运行时间，除掉函数中调用子函数的运行时间; \n",
    "\n",
    "percall:(第一个percall)等于 tottime/ncalls;\n",
    "\n",
    "cumtime:表示该函数及其所有子函数的调用运行的时间，即函数开始调用到返回的时间;\n",
    "\n",
    "percall:(第二个perca11)即函数运行一次的平均时间，等于cumtime/ncalls; \n",
    "\n",
    "filename:lineno(function):每个函数调用的具体信息;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2c46b40",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
